﻿using System;
using System.Windows;
using Seagull.BarTender.Print;

namespace PrinterTemplate
{
    public class BarTenderExporter
    {
        public static void TestExport()
        {
            string templateName = @"C:\Users\16893\Documents\BarTender\BarTender Documents\DP23.btw";
            string outputPath = @"C:\Users\16893\Documents\BarTender\BarTender Documents\label.png";
            // 确保输出目录存在
            ExportLabelToPng(templateName, outputPath);
        }

        public static void ExportLabelToPng(string btFilePath, string outputPngPath)
        {
            Engine? engine = null;
            try
            {
                // 创建 BarTender 引擎（需要管理员权限）
                engine = new Engine(true);
            
                // 打开标签文件
                LabelFormatDocument format = engine.Documents.Open(btFilePath);
                format.ExportImageToFile(outputPngPath, ImageType.PNG, ColorDepth.ColorDepth24bit, new Resolution(300),OverwriteOptions.Overwrite);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误: {ex.Message}");
            }
            finally
            {
                // 必须关闭引擎
               engine.Dispose();
            }
        }
    }
}